<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Shouyouba
 *
 * @author dragon.hu add @2016.2.24
 */
require_once 'BaseChannel.php';

//TT语音(手游吧)
class Shouyouba implements BaseChannel 
{
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="7a949018a107ce14560ecccd71b2054e"; // 充值秘钥
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("shouyouba", $_REQUEST);
		
		$sign = "";
		foreach(getallheaders() as $k => $v) 
		{
			if ($k == 'sign')
			{
				$sign = $v;  //获取传过来的sign
			}
		}
		$getdata = file_get_contents('php://input');
		$orderLog->log("shouyouba", "all POST data:".$getdata);
		/*$uid = $_REQUEST['uid'];  //用户ID
		$gameId = $_REQUEST['gameId'];  //游戏ID
		$sdkOrderId = $_REQUEST['sdkOrderId'];  //SDK订单号
		$cpOrderId = $_REQUEST['cpOrderId'];  //CP订单号
		$payFee = $_REQUEST['payFee'];  //实际支付金额
		$payResult = $_REQUEST['payResult'];  //支付结果
		$payDate = $_REQUEST['payDate'];  //支付时间
		$exInfo = $_REQUEST['exInfo'];  //CP扩展信息*/
		
		$getarr = json_decode(urldecode($getdata), true);
		$uid = $getarr['uid'];  //用户ID
		$gameId = $getarr['gameId'];  //游戏ID
		$sdkOrderId = $getarr['sdkOrderId'];  //SDK订单号
		$cpOrderId = $getarr['cpOrderId'];  //CP订单号
		$payFee = $getarr['payFee'];  //实际支付金额
		$payResult = $getarr['payResult'];  //支付结果
		$payDate = $getarr['payDate'];  //支付时间
		$exInfo = $getarr['exInfo'];  //CP扩展信息
		
		$mysign = urldecode($getdata).$paymentKey;
		
		if($sign == base64_encode(md5($mysign, true))) 
		{ //验证通过
			$tmpstatus = STATUS_SUCCESS;
			 if ($payResult != '1')
			 {
			 	$tmpstatus = STATUS_FAILD;
			 }
			//进行发货
			if(false == $orderLog->recordOrder($tmpstatus, $payFee, $cpOrderId, "shouyouba", $platform,$sdkOrderId))
			{
				$ret['head']['result'] = '1';
				$ret['head']['message'] = urlencode("失败");
				$orderLog->log("shouyouba",array("order"=>$cpOrderId,"money"=>$payFee,"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret['head']['result'] = '0';
				$ret['head']['message'] = urlencode("成功");
				$orderLog->log("shouyouba",array("order"=>$cpOrderId,"money"=>$payFee,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$ret['head']['result'] = '2';
			$ret['head']['message'] = urlencode("sign验证失败");
			$orderLog->log("shouyouba","  order:$cpOrderId  sign error sign:$sign mysign:$mysign");
		}
		$Ret = json_encode($ret);
		return urldecode($Ret);
	}
	
}